package com.hisense.hiioc.formtable.controller;

import com.github.pagehelper.PageInfo;
import com.hisense.hiioc.formtable.domain.Form;
import com.hisense.hiioc.formtable.domain.FormAndRows;
import com.hisense.hiioc.formtable.service.FieldService;
import com.hisense.hiioc.formtable.service.FormService;
import com.hisense.hiioc.formtable.util.ResponseResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class FormController {

    @Autowired
    private FormService formService;

    @ApiOperation(value="新增表单", notes="填写表单名称和表单描述新增表单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formName", value = "表单名称", required = true, dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "formDesc", value = "表单描述", dataType = "String" ,paramType = "query")
    })
    @PostMapping("/form")
    public ResponseResult addForm(
            @RequestParam(value = "formName", defaultValue = "") String formName,
            @RequestParam(value = "formDesc", defaultValue = "") String formDesc) {
        ResponseResult result = new ResponseResult();
        if ("".equals(formName) || formName == null) {
            return ResponseResult.Builder.buildError("表单名称不能为空!");
        } else {
            Form form = new Form();
            form.setFormName(formName);
            form.setFormDesc(formDesc);
            int num = formService.insert(form);
            if (num > 0) {
                result.setData(200);
                result.setMessage("新增自定义表单成功！");
                result.setStatus(true);
            } else if (num == -1){
                return ResponseResult.Builder.buildError("表单名称已存在");
            } else {
                return ResponseResult.Builder.buildError("新增表单失败!");
            }
        }
        return result;
    }

    @ApiOperation(value="查询表单列表", notes="查询表单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchValue", value = "模糊搜索内容", required = false, dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "Integer" ,paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true, dataType = "Integer" ,paramType = "query")
    })
    @GetMapping("/form")
    public ResponseResult queryAllForm(
            @RequestParam(value = "searchValue", defaultValue = "") String searchValue,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size    ) {
        ResponseResult result = new ResponseResult();
        PageInfo<FormAndRows> pageInfo = formService.queryFormAndRows(searchValue,page, size);

        if (pageInfo != null) {
            result.setData(pageInfo);
            result.setMessage("查询表单列表成功！");
            result.setStatus(true);
        } else {
            return ResponseResult.Builder.buildError("查询表单列表失败!");
        }

        return result;
    }

    @ApiOperation(value = "查询单个表单详情", notes = "查询单个表单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单Id", required = true, dataType = "Integer", paramType = "path")
    })
    @GetMapping("/form/{formId}")
    public ResponseResult queryOneForm(@PathVariable Integer formId) {
        ResponseResult result = new ResponseResult();
        if (formId == null ) {
            return ResponseResult.Builder.buildError("请输入正确的表单ID");
        } else {
            Form form = formService.queryOne(formId);
            if (form == null) {
                return ResponseResult.Builder.buildError("查询的表单不存在");
            } else  {
                result.setMessage("查询单个表单成功！");
                result.setStatus(true);
                result.setData(form);
            }
        }
        return result;
    }

    @ApiOperation(value = "删除表单", notes = "删除表单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单Id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/form/{formId}")
    public ResponseResult delForm(@PathVariable Integer formId) {
        ResponseResult result = new ResponseResult();
        if (formId == null) {
            return ResponseResult.Builder.buildError("请输入正确的表单ID");
        } else {
            System.out.println("formId = " + formId);
            int num = formService.deleteById(formId);
            if (num > 0) {
                result.setStatus(true);
                result.setMessage("删除表单成功！");
            } else if (num == -1){
                return ResponseResult.Builder.buildError("表单中有数据无法删除！");
            } else {
                return ResponseResult.Builder.buildError("删除失败！");
            }
        }
        return result;
    }

    @ApiOperation(value = "更新表单", notes = "更新表单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "formId", value = "表单Id", required = true, dataType = "Integer" ,paramType = "query"),
            @ApiImplicitParam(name = "formName", value = "表单名称", required = true, dataType = "String" ,paramType = "query"),
            @ApiImplicitParam(name = "formDesc", value = "表单描述", dataType = "String" ,paramType = "query")
    })
    @PutMapping("/form")
    public ResponseResult updateForm(
            @RequestParam(value = "formId") Integer formId,
            @RequestParam(value = "formName", defaultValue = "") String formName,
            @RequestParam(value = "formDesc", defaultValue = "") String formDesc) {
        if (formName == "" || formName == null) {
            return ResponseResult.Builder.buildError("表单名称不能为空！");
        } else  {
            Form form = new Form();
            form.setFormId(formId);
            form.setFormName(formName);
            form.setFormDesc(formDesc);
            formService.update(form);
        }
        return ResponseResult.Builder.buildOk("更新成功！");
    }


}
